package com.wjyf.reducing_bento.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.model.dto.CommodityAddDto;
import com.wjyf.reducing_bento.model.dto.CommodityUpdateDto;
import com.wjyf.reducing_bento.model.entity.CommodityBean;
import com.wjyf.reducing_bento.exception.BusinessException;
import com.wjyf.reducing_bento.mapper.CommodityMapper;
import com.wjyf.reducing_bento.service.CommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author wjyf
 * @since 2023-03-14 08:36:32
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, CommodityBean> implements CommodityService {

    @Override
    public Page<CommodityBean> getCommodityPage(Long current, Long pageSize) {
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return page(new Page<CommodityBean>(current, pageSize));
    }

    @Override
    public boolean add(CommodityAddDto commodityAddDto) {
        if (commodityAddDto == null){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String codName = commodityAddDto.getCodName();
        Double codPrice = commodityAddDto.getCodPrice();
        Integer codStock = commodityAddDto.getCodStock();
        String codDetails = commodityAddDto.getCodDetails();
        if (StringUtils.isAnyBlank(codName, codDetails) || codPrice <= 0 || codStock <= 0){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CommodityBean commodityBean = new CommodityBean();
        BeanUtil.copyProperties(commodityAddDto, commodityBean);
        commodityBean.setCreateTime(new Date()).setModifyTime(new Date());
        return save(commodityBean);
    }

    @Override
    public boolean update(CommodityUpdateDto commodityUpdateDto) {
        if (commodityUpdateDto == null){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer codId = commodityUpdateDto.getCodId();
        String codName = commodityUpdateDto.getCodName();
        Double codPrice = commodityUpdateDto.getCodPrice();
        Integer codStock = commodityUpdateDto.getCodStock();
        String codDetails = commodityUpdateDto.getCodDetails();
        if (StringUtils.isAnyBlank(codName, codDetails) || codPrice <= 0 || codStock <= 0 || codId <= 0){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CommodityBean commodityBean = new CommodityBean();
        BeanUtil.copyProperties(commodityUpdateDto, commodityBean);
        commodityBean.setModifyTime(new Date());
        return updateById(commodityBean);
    }
}
